#include <gst/interfaces/xoverlay.h>
#include <gtk/gtk.h>

#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>  // for GDK_WINDOW_XID
#endif

gint
destroy(GtkWidget *widget,GdkEvent *event,gpointer data) {
	g_print("destroy\n");
	gtk_main_quit();
	return TRUE;
}

gboolean
bus_call(GstBus * bus, GstMessage *msg, gpointer data) {
	GtkWidget *window = (GtkWidget*) data;
	gchar *debug;
	GError *error;
	g_debug("bus_call %d",GST_MESSAGE_TYPE(msg));
	switch(GST_MESSAGE_TYPE(msg)) {
		case GST_MESSAGE_EOS:
			g_debug("end of stream");
			break;
		case GST_MESSAGE_ELEMENT:
			if (gst_structure_has_name(msg->structure, "prepare-xwindow-id")) {
				g_debug("prepare-xwindow-xid");
				gst_x_overlay_set_xwindow_id(GST_X_OVERLAY(GST_MESSAGE_SRC(msg)), GDK_WINDOW_XID(gtk_widget_get_window(window)));
				return TRUE;
			}
			return FALSE;
			break;
		case GST_MESSAGE_ERROR :
			gst_message_parse_error(msg,&error,&debug);
			g_debug("error:%s debug:%s",error->message,debug);
			g_free(debug);
			g_error_free(error);
			gtk_main_quit();
			break;	
		default:
			break;
    }
	return TRUE;
}

GstElement *
create_pipeline(GtkWidget *area) {
	GstElement *pipeline,*source,*sink;
	g_debug("create_pipeline");
	pipeline=gst_pipeline_new("test");
	source=gst_element_factory_make("videotestsrc","play");
	g_object_set(G_OBJECT(source),"horizontal-speed",1,NULL);
	sink=gst_element_factory_make("xvimagesink","sink");
	if (!pipeline || !source || !sink ) {
		g_critical("Internal error\n");
		return NULL;
	}

   gst_bus_add_watch(gst_pipeline_get_bus(GST_PIPELINE(pipeline)),(GstBusFunc) bus_call, area);	
	gst_bin_add_many(GST_BIN(pipeline),source,sink,NULL);
	gst_element_link(source,sink);

	return pipeline;
}

int
main(int argc,char *argv[]) {
	GError *error=NULL;
	GtkWidget *window;
	GtkWidget *area;
	GstElement *pipeline;
	
	gst_init(&argc,&argv);
	gtk_init(&argc,&argv);
	
	window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
	area=gtk_drawing_area_new();
	
	gtk_container_add(GTK_CONTAINER(window),area);	

	g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(destroy),NULL);

	pipeline=create_pipeline(area);

	gtk_widget_show_all(window);
	gst_element_set_state(pipeline, GST_STATE_PLAYING);

	gtk_main();

	return(0);
}

